//
//  EFADangerTCell.m
//  efairy
//
//  Created by XiangDongMBP on 2017/11/7.
//  Copyright © 2017年 East. All rights reserved.
//

#import "EFADangerTCell.h"
#import "EFANoticePubCCell.h"
#import "EFAModel.h"
#import <UIImageView+WebCache.h>
#import "UIColor+XDHex.h"

@interface EFADangerTCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic,   strong) UIImage *defalutImg;
@property (nonatomic,   strong) UIImage *portraitImg;
@property (nonatomic, readonly) NSArray *imgUrlArray;
@end

static NSString *const kReuseCellId = @"EFANoticePubCCell";

@implementation EFADangerTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.defalutImg = [UIImage imageNamed:@"global_img_default"];
    self.portraitImg = [UIImage imageNamed:@"global_portrait"];
    
//    self.portraitImgView.layer.masksToBounds = YES;
//    self.portraitImgView.layer.cornerRadius = 3;
    
    UINib *cellNib = [UINib nibWithNibName:kReuseCellId bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:kReuseCellId];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgUrlArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    EFANoticePubCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kReuseCellId forIndexPath:indexPath];
    cell.closeBtn.hidden = YES;
    cell.contentImgView.contentMode = UIViewContentModeScaleAspectFill;
    if (indexPath.row < self.imgUrlArray.count) {
        NSString *urlStr = self.imgUrlArray[indexPath.row];
        [cell.contentImgView sd_setImageWithURL:[NSURL URLWithString:urlStr]
                               placeholderImage:self.defalutImg
                                      completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                                          ;
                                      }];
    }
    return cell;
}

//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionHeader]) {
//        EFANoticeEditCRView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kReuseCRViewId forIndexPath:indexPath];
//        header.pubModel = self.pubModel;
//        return header;
//    }
//    return nil;
//}

#pragma mark - <UICollectionViewDelegateFlowLayout>
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(CGRectGetWidth(self.view.bounds), 254.0);
//}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 2 * 15) / 3.0;
    return CGSizeMake(width, width);
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(dangerTCell:imgViewTapped:indexPath:)]) {
        NSMutableArray *imgViewMArray = [NSMutableArray arrayWithCapacity:self.imgUrlArray.count];
        for (NSInteger i = 0; i < self.imgUrlArray.count; i++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
            EFANoticePubCCell *cell = (EFANoticePubCCell *)[collectionView cellForItemAtIndexPath:indexPath];
            if (cell) {
                [imgViewMArray addObject:cell.contentImgView];
            }
        }
        [self.delegate dangerTCell:self imgViewTapped:imgViewMArray indexPath:indexPath];
    }
}

- (void)configViewWithDangerListItem:(EFADangerListItem *)item {
    self.item = item;
    
    if (item.efairyhiddendanger_is_fixed) {
        [self.portraitImgView sd_setImageWithURL:[NSURL URLWithString:item.efairyuser_headimg_url_fixed_user] placeholderImage:self.portraitImg];
        self.nameLabel.text = item.efairyuser_nickname_fixed_user;
        self.stateLabel.text = @"隐患已排查";
        self.stateLabel.textColor = [UIColor xd_colorWithHex:0x60C256];
        self.dateLabel.text = item.efairyhiddendanger_fixed_time ? item.efairyhiddendanger_fixed_time : item.efairyhiddendanger_add_time;
    } else {
        [self.portraitImgView sd_setImageWithURL:[NSURL URLWithString:item.efairyuser_headimg_url] placeholderImage:self.portraitImg];
        self.nameLabel.text = item.efairyuser_nickname;
        self.stateLabel.text = @"隐患未排查";
        self.stateLabel.textColor = [UIColor xd_colorWithHex:0xFDA03C];
        self.dateLabel.text = item.efairyhiddendanger_add_time;
    }
    self.pjtNameLabel.text = item.efairyproject_name;
    self.titleLabel.text = item.efairyhiddendanger_title;
    self.contentLabel.text = item.efairyhiddendanger_content;
    
    NSInteger row = item.efairyhiddendanger_imgurl_list.count / 3 + ((item.efairyhiddendanger_imgurl_list.count % 3) ? 1 : 0);
    CGFloat cellWidth = (CGRectGetWidth([UIScreen mainScreen].bounds) - 2*15 - 2 * 15) / 3.0;
    NSInteger column = item.efairyhiddendanger_imgurl_list.count > 3 ? 3 : item.efairyhiddendanger_imgurl_list.count;
    CGFloat width = cellWidth * column + 2 * 15;
    CGFloat height = cellWidth * row + (row - 1) * 15;
    if (!row) {
        height = 0;
    }
    self.collectionViewWidth.constant = width;
    self.collectionViewHeight.constant = height;
    self.collectionView.scrollEnabled = NO;
    [self.collectionView reloadData];
}

#pragma mark - getters and setters
- (NSArray *)imgUrlArray {
    return self.item.efairyhiddendanger_imgurl_list;
}

@end
